package src.student;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.FieldPosition;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;

public class TestClass {

	public  void testClass() throws FileNotFoundException, IOException{
		Scanner scanner  = new Scanner(System.in);
		
		List<Student> studentlist= new ArrayList<Student>();
		Properties  pero = new Properties();
		pero.load(new FileInputStream("studnet.txt"));
		Set<String> stringPropertyNames = pero.stringPropertyNames();
		for (String key : stringPropertyNames) {
				String property = pero.getProperty(key);
				String[] split = property.split(",");
				
				Student  stu = new Student();
					stu.setName(split[0]);
					stu.setAge(split[1]);
					stu.setCalss(split[2]);
					
					studentlist.add(stu);
		}

		List<DomeClass>  classlist = new ArrayList<DomeClass>();
		Properties  perr = new Properties();
		perr.load(new FileInputStream("class.txt"));
		Set<String> stringPropertyNames2 = perr.stringPropertyNames();
		for (String key : stringPropertyNames2) {
			String value = perr.getProperty(key);
			String[] split = value.split(",");
			DomeClass dome  = new DomeClass();
				dome.setDomeclass(split[0]);
				dome.setTeacher(split[1]);
			
				classlist.add(dome);
		}
		
		
		while (true) {
			System.out.println("1:新增班级，2：查找班级，3：按班级查学生，4：修改班级，5：删除班级，6；退出");
			int i = scanner.nextInt();
			if (i==1) {
					DomeClass  domeclass = new DomeClass();
					System.out.println("输入班级");
					domeclass.setDomeclass(scanner.next());
					System.out.println("输入这个班级的老师");
					domeclass.setTeacher(scanner.next());
					
					classlist.add(domeclass);
			}
			if (i==2) {
				Iterator<DomeClass> iterator = classlist.iterator();
				while (iterator.hasNext()) {
					DomeClass domeClass = (DomeClass) iterator.next();
						System.out.println(domeClass);
				}
			}
			if (i==3) {
				System.out.println("输入你要查询的班级");
				String  tempclass = scanner.next();
				Iterator<Student> iterator = studentlist.iterator();
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					if (tempclass.equals(student.getCalss())) {
						System.out.println(student);
					}
				}
			}
			if (i==4) {
				System.out.println("输入你要修改的学生");
				String doeclass = scanner.next();
				Iterator<DomeClass> iterator = classlist.iterator();
				int p  =0;
				while (iterator.hasNext()) {
					DomeClass domeClass = (DomeClass) iterator.next();
					if (domeClass.equals(domeClass)) {
							System.out.println("输入新的姓名");
							domeClass.setDomeclass(scanner.next());
							System.out.println("修改成功");
							p++;
					}
				}
				if (p==0) {
					System.out.println("没有这个班级");
				}
			}
			if (i==5) {
				System.out.println("输入你要删除的班级");
				String neme  = scanner.next();
				Iterator<DomeClass> iterator = classlist.iterator();
				DomeClass  temp = null;
				while (iterator.hasNext()) {
					DomeClass domeClass = (DomeClass) iterator.next();
						if (neme.equals(domeClass.getDomeclass())) {
							 temp = domeClass;
						}
				}
				classlist.remove(temp);
				System.out.println("删除成功");
			}
			if (i==6) {
					Properties  per = new Properties();
					Iterator<DomeClass> iterator = classlist.iterator();
					int b = 0;
					while (iterator.hasNext()) {
						DomeClass domeClass = (DomeClass) iterator.next();
						per.setProperty("C"+b++,a(domeClass));
						
					}
					per.store(new FileOutputStream("class.txt"), "");
					break;
			}
		}
	}

	private String a(DomeClass domeClass) {
		return domeClass.getDomeclass()+","+domeClass.getTeacher();
	}
}
